package com.example.starapp;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioGroup;
import com.animee.starapp.starfrag.StarInfo;
import com.example.starapp.luckfrag.LuckFragment;
import com.example.starapp.mefrag.MeFragment;
import com.example.starapp.parnterfrag.ParnterFragment;
import com.example.starapp.starfrag.StarFragment;
import com.example.starapp.utils.AssetsUtils;
import com.google.gson.Gson;

import java.util.List;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    RadioGroup mainRg;
    Fragment starFrag,parnterFrag,luckFrag,meFrag;  //声明放入到activity当中的4个fragment的名称
    private FragmentManager fm;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        hideDecorView();
        /*查找控件*/
        mainRg = findViewById(R.id.main_rg);
        mainRg.setOnCheckedChangeListener(this);    //设置监听器，监听哪个单选按钮被点击了

        StarInfo starInfo = loadData();//获取Assets文件夹当中的json数据
        /*四个fragment进行初始化*/
        Bundle bundle = new Bundle();
        bundle.putSerializable("info",starInfo);
        // 因为三个fragment都展示同一个数据源的数据，所以没有必要访问三次，就访问一次，在创建fragment时，将数据放进去
        starFrag = new StarFragment();
        starFrag.setArguments(bundle);
        parnterFrag = new ParnterFragment();
        parnterFrag.setArguments(bundle);
        luckFrag = new LuckFragment();
        luckFrag.setArguments(bundle);

        meFrag = new MeFragment();
//        将四个Fragment一起加入到要替换的布局中
        initFragmentPage();

    }
    public StarInfo loadData(){
        //读取assets/xzcontent/xzcontent.json文件
        String json = AssetsUtils.getJsonByAssets(this, "xzcontent/xzcontent.json");
//        将json字符串转换成创建类的对象
        Gson gson = new Gson();
        StarInfo starInfo = gson.fromJson(json, StarInfo.class);
        return starInfo;
    }
    /*  将四个Fragment一起加入到要替换的布局中*/
    private void initFragmentPage() {
        //        1.创建碎片管理器对象
        fm = getSupportFragmentManager();
//        2.创建碎片处理事务对象
        FragmentTransaction tran = fm.beginTransaction();
//        3.将四个fragment统一添加到布局中
        tran.add(R.id.main_layout_center,starFrag);
        tran.add(R.id.main_layout_center,parnterFrag);
        tran.add(R.id.main_layout_center,luckFrag);
        tran.add(R.id.main_layout_center,meFrag);
//        4.隐藏其中的三个
        tran.hide(parnterFrag);
        tran.hide(luckFrag);
        tran.hide(meFrag);
//        5.提交碎片改变后的事务
        tran.commit();
    }

    public void hideDecorView(){
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setNavigationBarColor(Color.TRANSPARENT);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        FragmentTransaction tran = fm.beginTransaction();

        switch (checkedId) {
            case R.id.main_rb_star:
                tran.hide(parnterFrag);
                tran.hide(luckFrag);
                tran.hide(meFrag);
                tran.show(starFrag);
                break;
            case R.id.main_rb_parnter:
                tran.hide(starFrag);
                tran.hide(luckFrag);
                tran.hide(meFrag);
                tran.show(parnterFrag);
                break;
            case R.id.main_rb_luck:
                tran.hide(starFrag);
                tran.hide(parnterFrag);
                tran.hide(meFrag);
                tran.show(luckFrag);
                break;
            case R.id.main_rb_me:
                tran.hide(starFrag);
                tran.hide(luckFrag);
                tran.hide(parnterFrag);
                tran.show(meFrag);
                break;
        }
        tran.commit();
    }


}